﻿#region Imports
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using CommonUtilities.Extensions;
using CommonUtilities.Messages;
using DirectoryAnalyzer.Events;
using DirectoryAnalyzer.Main;
using DirectoryAnalyzer.ResourceFiles;
#endregion

namespace DirectoryAnalyzer.Views
{
    public partial class frmPathSelector : Form
    {
        // TEST - RKD
        public static string TestPath
        {
            get
            {
                string path;
                path = @"Z:\Files -&- Folders\[softies] -2\An-DROID";
                //path = @"C:\Documents and Settings\rhythm divine\My Documents\Visual Studio 2010\Projects\_NEW_KOL";
                //path = Path.GetDirectoryName(Path.GetDirectoryName(Path.GetDirectoryName(Application.ExecutablePath))) + @"\_Test";
                path = @"C:\Documents and Settings\rhythm divine\My Documents\Visual Studio 2010\Projects\DirectoryAnalyzer\DirectoryAnalyzer\_Test";
                path = @"C:\Documents and Settings\rhythm divine\My Documents\Visual Studio 2010\Projects\DirectoryAnalyzer\DirectoryAnalyzer\_APK";

                return path;
            }
        }

        #region Declarations
        string sBasePath = "";
        #endregion

        #region Constructors
        public frmPathSelector()
        {
            InitializeComponent();

            cmbFilters.DataSource = "*;jpg;mp3;txt;doc".Split(';')
                .Select(extn => !extn.Contains("*") || extn == "*" ? "*." + extn : extn).ToObservableCollection();

            txtPath.Focus();
            txtPath.Text = frmPathSelector.TestPath;
            txtPath_Leave(null, null);
        }
        #endregion

        #region Events
        private void btnBrowsePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dlgBrowseFolder = new FolderBrowserDialog
            {
                RootFolder = Environment.SpecialFolder.MyComputer,
                ShowNewFolderButton = true,
                Description = "Select folder to analyze..",
                SelectedPath = Directory.Exists(txtPath.Text) ? txtPath.Text : ""
            };

            dlgBrowseFolder.ShowDialog(this);
            txtPath.Text = dlgBrowseFolder.SelectedPath;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //Extension

            if (!Directory.Exists(txtPath.Text)) MessageBoxUtil.Show(Program.IsDebug, ResourceDirectoryAnalyzer.Msg_InvalidPath, ResourceDirectoryAnalyzer.App_InvalidPath);
            else
            {
                frmDuplicateFinder owner = (frmDuplicateFinder)this.Owner;
                if (owner.lstPath.Items.Cast<string>().ToList().BinarySearch(txtPath.Text) < 0) // Search if already added
                {
                    owner.RefreshButtons(Enumerations.eRefreshType.Add);
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    switch (MessageBoxUtil.Show(Program.IsDebug, ResourceDirectoryAnalyzer.Msg_DuplicatePath, ResourceDirectoryAnalyzer.App_Validation, MessageBoxButtons.YesNo, MessageBoxIcon.Error))
                    {
                        case DialogResult.Yes:
                            this.DialogResult = DialogResult.Cancel;
                            this.Close();
                            break;

                        case DialogResult.No:
                        default:
                            break;
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtPath_TextChanged(object sender, EventArgs e)
        {
            sBasePath = AddressFormatEvent._TextChanged(sender);
            bool isValid = (Directory.Exists(sBasePath) && txtPath.Text.Length > 0);
            btnOk.Enabled = isValid;
            btnOk.ForeColor = isValid ? Color.Black : Color.Red;
        }

        private void txtPath_Leave(object sender, EventArgs e)
        {
            AddressLeaveEvent._TextLeave(sender, sBasePath);
            btnOk.Enabled = Directory.Exists(txtPath.Text);
        }
        #endregion
    }
}
